package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.DateListAdapter;
import cn.yeyiu.sexyiu.listener.OnItemClickListener;
import cn.yeyiu.util.ExternalJSONFile;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.videoplayer.VideoPlayerActivity;

public class UserDataActivity extends AppCompatActivity {
    private DateListAdapter adapter;
    private ImageView backBtn;
    private TextView backTv;
    private RecyclerView rv;
    private String type;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_other_list);
        dataInit();
        viewInit();
        adapterInit();
        eventInit();
        uiInit();
    }

    private void dataInit() {
        this.type = getIntent().getStringExtra("type");
        this.adapter = new DateListAdapter(this, this.type);
    }

    private void viewInit() {
        this.backBtn = (ImageView) findViewById(R.id.back_btn);
        this.backTv = (TextView) findViewById(R.id.back_tv);
        this.rv = (RecyclerView) findViewById(R.id.video_card_rv);
    }

    private void adapterInit() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        this.rv.setLayoutManager(linearLayoutManager);
        this.rv.setAdapter(this.adapter);
    }

    private void eventInit() {
        this.backBtn.setOnClickListener(v->{finish();});
        this.adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override // cn.yeyiu.sexyiu.listener.OnItemClickListener
            public void onItemClick(View view) {
                int childAdapterPosition = UserDataActivity.this.rv.getChildAdapterPosition(view);
                UserDataActivity userDataActivity = UserDataActivity.this;
                MediaUtil.playVideo(userDataActivity, VideoPlayerActivity.class, userDataActivity.adapter.dataList.get((UserDataActivity.this.adapter.dataList.size() - 1) - childAdapterPosition));
            }

            @Override // cn.yeyiu.sexyiu.listener.OnItemClickListener
            public void onItemLongClick(View view) {
                if (!ExternalJSONFile.favour.equals(UserDataActivity.this.type)) {
                    int childAdapterPosition = UserDataActivity.this.rv.getChildAdapterPosition(view);
                    int size = (UserDataActivity.this.adapter.dataList.size() - 1) - childAdapterPosition;
                    UserDataActivity userDataActivity = UserDataActivity.this;
                    ExternalJSONFile.remove(userDataActivity, userDataActivity.type, size);
                    UserDataActivity.this.adapter.dataList.remove(size);
                    UserDataActivity.this.adapter.notifyItemRemoved(childAdapterPosition);
                }
            }
        });
    }

    private void uiInit() {
        if ("favour".equals(this.type)) {
            this.backTv.setText(R.string.my_favour);
        } else {
            this.backTv.setText(R.string.my_history);
        }
    }
}
